# Contributor: Mika Havela <mika.havela@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
_luaversions="5.1 5.2 5.3 5.4"
pkgname=lua-expat
_name=expat
pkgver=1.5.1
pkgrel=1
pkgdesc="SAX XML parser based on the Expat library"
url="https://lunarmodules.github.io/luaexpat/"
arch="all"
license="MIT"
makedepends="expat-dev"
for _i in $_luaversions; do
	makedepends="$makedepends lua$_i-dev"
	subpackages="$subpackages lua$_i-$_name:_split${_i/./}"
done

source="$pkgname-$pkgver.tar.gz::https://github.com/lunarmodules/luaexpat/archive/refs/tags/$pkgver.tar.gz"
builddir="$srcdir/luaexpat-$pkgver"
options="!check"

build() {
	cd "$srcdir"
	for _i in $_luaversions; do
		cp -r "$builddir" "$srcdir"/build-$_i
		cd "$srcdir"/build-$_i
		make LUA_V=$_i
	done
}

package() {
	for _i in $_luaversions; do
		(cd "$srcdir"/build-$_i && make install LUA_V=$_i DESTDIR="$pkgdir")
	done
}

_split() {
	local d= _ver=$1
	pkgdesc="$pkgdesc for Lua $_ver"
	install_if="lua$_ver $pkgname=$pkgver-r$pkgrel"
	depends=
	for d in usr/lib/lua usr/share/lua; do
		if [ -d "$pkgdir"/$d/$_ver ]; then
			mkdir -p "$subpkgdir"/$d
			mv "$pkgdir"/$d/$_ver "$subpkgdir"/$d/
		fi
	done
}

for _i in $_luaversions; do
	eval "_split${_i/./}() { _split $_i; }"
done

sha512sums="
45834b02d863a65c6de2ad3c5c671935ae512dd906233a647269438aeeb37c7bbd6e7166aefd8da72480b33e1aa79a86ed070ed8800a8e0f303ccc74918f3bb8  lua-expat-1.5.1.tar.gz
"
